Imports ModuloAdministraciondeEnvios.BusinessLogicLayer
Imports ModuloAdministraciondeEnvios.DataAccessLayer
Imports ModuloAdministraciondeEnvios.Entity

Namespace PresentationLayer
    Public Class FrmClientes


        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

            InicializarFormulario()

        End Sub

        Private Sub FrmClientes_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated



            TextBoxNombre.Text = ClienteNombre
            TextBoxDomicilio.Text = ClienteDomicilio
          
            If ClienteCUIT <> "" Then
                If ClienteCUIT.Length() > 9 Then

                    RadioButtonResponsableIncripto.Checked = True
                Else
                    RadioButtonConsumidorFinal.Checked = True
                End If

                MaskedTextBoxCUIT.Text = ClienteCUIT

            End If

        End Sub

        Private Sub FrmClientes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            TraerLocalidades()
            InicializarFormulario()

        End Sub

        Sub TraerLocalidades()

            Dim Localidades As ILocalidad = DirectCast(New DataLocalidad, ILocalidad)
            Dim ResultadoLocalidades As DataSet = Localidades.TraerLocalidadesTodas()

            With ComboBoxLocalidades
                .DataSource = ResultadoLocalidades.Tables(0)
                .DisplayMember = "Nombre"
                .ValueMember = "IDLocalidad"
            End With

            ComboBoxLocalidades.SelectedIndex = 0

        End Sub


        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            If RadioButtonConsumidorFinal.Checked Then
                If ValidarDocumento(MaskedTextBoxCUIT.Text) Then
                    ErrorClienteCUIT.Clear()
                Else
                    MsgBox("Documento NO Valido")
                    ErrorClienteCUIT.SetError(MaskedTextBoxCUIT, "Debe ser un CUIT valido")
                    ErrorClienteCUIT.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
                    MaskedTextBoxCUIT.Focus()
                    Exit Sub
                End If
            End If


            If RadioButtonResponsableIncripto.Checked Then
                If ValidarCUIT(MaskedTextBoxCUIT.Text.Replace("-", "")) Then
                    ErrorClienteCUIT.Clear()
                Else
                    MsgBox("CUIT NO Valido")
                    ErrorClienteCUIT.SetError(MaskedTextBoxCUIT, "Debe ser un Documento valido")
                    ErrorClienteCUIT.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
                    Exit Sub
                End If
            End If


            If TextBoxNombre.Text.Length < 10 Then
                MsgBox("Longitud Nombre Insulficiente" & vbCrLf & "Debeser mayor a 12 Caracteres " & vbCrLf & "Por favor revisar")
                ErrorClienteNombre.SetError(TextBoxNombre, "Longitud Insuficente")
                ErrorClienteCUIT.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
                TextBoxNombre.Focus()
                Exit Sub
            Else
                ErrorClienteNombre.Clear()
            End If


            If TextBoxDomicilio.Text.Length < 10 Then
                MsgBox("Longitud Nombre Insulficiente" & vbCrLf & "Debe ser mayor a 15 Caracteres " & vbCrLf & "Por favor revisar")
                ErrorClienteNombre.SetError(TextBoxDomicilio, "Longitud Insuficente")
                ErrorClienteCUIT.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
                TextBoxDomicilio.Focus()
                Exit Sub
            Else
                ErrorClienteDomicilio.Clear()
            End If



            Dim Cliente As New DataCliente

            Try

                Cliente.Nombre = UCase(TextBoxNombre.Text).Replace("'", "")
                Cliente.Domicilio = UCase(TextBoxDomicilio.Text).Replace("'", "")
                Cliente.Localidad = ComboBoxLocalidades.SelectedValue
                Cliente.CUIT = MaskedTextBoxCUIT.Text

                Dim Grabar As ICliente = DirectCast(New DataCliente, ICliente)
                Dim Resultado As Boolean = Grabar.AgregarCliente(Cliente)


            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

            InicializarFormulario()

        End Sub

        Sub InicializarFormulario()

            TextBoxNombre.Text = ""
            TextBoxDomicilio.Text = ""
            MaskedTextBoxCUIT.Text = ""
            ComboBoxLocalidades.SelectedIndex = 0
            RadioButtonResponsableIncripto.Checked = True

            CheckCondicionFiscal()


        End Sub


        Sub CheckCondicionFiscal()

            If RadioButtonConsumidorFinal.Checked Then
                MaskedTextBoxCUIT.Mask = "00000000"
            End If

            If RadioButtonResponsableIncripto.Checked Then
                MaskedTextBoxCUIT.Mask = "00-00000000-0"
            End If

        End Sub


        Private Sub RadioButtonConsumidorFinal_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonConsumidorFinal.CheckedChanged

            CheckCondicionFiscal()

        End Sub

        Private Sub RadioButtonResponsableIncripto_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonResponsableIncripto.CheckedChanged

            CheckCondicionFiscal()
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


            If ValidarCUIT(MaskedTextBoxCUIT.Text.Replace("-", "")) = True Then
                MsgBox("CUIT VALIDO")
            Else
                MsgBox("CUIT NO VALIDO")

            End If

        End Sub

        Private Sub ComboBoxLocalidades_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxLocalidades.SelectedIndexChanged


            If IsNumeric(ComboBoxLocalidades.SelectedValue.ToString) Then

                Dim Localidad As ILocalidad = DirectCast(New DataLocalidad, ILocalidad)
                Dim Resultado As String = Localidad.TraerCodigoPostalDeLocalidad(ComboBoxLocalidades.SelectedValue)
                LabelCodigoPostal.Text = Resultado
            End If


        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim BuscarCliente As New FrmBuscaPorCliente
            BuscarCliente.ShowDialog()

        End Sub




    End Class
End Namespace